import router from "@/router";
import store from "@/store";
import ACCESS_ENUM from "@/access/accessEnum";

router.beforeEach(async (to, from, next) => {
  let loginUser = store.state.user.loginUser;
  if (!loginUser || !loginUser.userRole) {
    await store.dispatch("user/getLoginUser");
    loginUser = store.state.user.loginUser;
  }
  console.log("当前用户登录信息", store.state.user.loginUser);
  const needAccess = (to.meta?.access as string) ?? ACCESS_ENUM.NOT_LOGIN;
  console.log("当前访问需要的身份==>", needAccess);
  // 需要登录
  if (needAccess !== ACCESS_ENUM.NOT_LOGIN) {
    // 需要管理员
    if (
      !loginUser ||
      !loginUser.userRole ||
      loginUser.userRole == ACCESS_ENUM.NOT_LOGIN
    ) {
      return;
    }
    // 如果已经登陆了，但是权限不足，那么跳转到无权限页面
    // if (!checkAccess(loginUser, needAccess)) {
    //   console.log("当前登录用户的权限", needAccess);
    //   next("/noAuth");
    //   return;
    // }
  }
  next();
});
